<?php

namespace api\modules\v1\controllers\base;

use common\helpers\CommonHelper;
use common\models\base\ToolCategory;
use services\base\ToolCategoryService;
use Yii;
use api\controllers\OnAuthController;

class ToolCategoryController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/base/tool-category/index 素材分类列表
     * @apiGroup ToolCategory
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Object}  data.list             列表数据
     * @apiSuccess {Number}  data.list.id               列表ID
     * @apiSuccess {Number}  data.list.pid              父级ID
     * @apiSuccess {String}  data.list.name             名称
     * @apiSuccess {Number}  data.list.status           状态
     * @apiSuccess {Number}  data.list.is_builtin       是否内置【1：是 0：否】，内置则不可编辑删除
     * @apiSuccess {String}  data.list.status_name      状态名称
     * @apiSuccess {Number}  data.list.sort             排序
     * @apiSuccess {array}  data.list.children         子集分类array[]
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $service = new ToolCategoryService();
        $list = $service->toolCategoryList($page, $pageSize);
        $list['condition'] = ToolCategoryService::getEditCondition(null, 0);
        return $list;
    }

    /**
     * @api {get} /v1/base/tool-category/info 素材分类详情
     * @apiGroup ToolCategory
     * @apiName info
     * @apiUse CommonParam
     *
     * @apiParam {int} id           素材分类ID
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Object}  data.list             列表数据
     * @apiSuccess {Number}  data.list.id               列表ID
     * @apiSuccess {Number}  data.list.pid              父级ID
     * @apiSuccess {String}  data.list.name             名称
     * @apiSuccess {Number}  data.list.status           状态
     * @apiSuccess {Number}  data.list.is_builtin       是否内置【1：是 0：否】
     * @apiSuccess {Number}  data.list.sort             排序
     *
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;
        $service = new ToolCategoryService();
        return $service->getInfo($id);
    }

    /**
     * @api {post} /v1/base/tool-category/create 新增素材分类
     * @apiGroup ToolCategory
     * @apiName create
     * @apiUse CommonParam
     *
     * @apiBody {String}   name                名称
     * @apiBody {Number}   pid                 父级ID
     * @apiBody {Number}   sort                排序
     * @apiBody {Array}    status              状态【0：禁用; 1：启用】
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();
        $params['id'] = 0;

        $service = new ToolCategoryService();
        return $service->updateToolCategory($params);
    }

    /**
     * @api {post} /v1/base/tool-category/update 更新素材分类
     * @apiGroup ToolCategory
     * @apiName update
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                  ID
     * @apiBody {String}   name                名称
     * @apiBody {Number}   pid                 父级ID
     * @apiBody {Number}   sort                排序
     * @apiBody {Array}    status              状态【0：禁用; 1：启用】
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();

        $params['id'] = $params['id'] ?? 0;
        $service = new ToolCategoryService();
        return $service->updateToolCategory($params);
    }

    /**
     * @api {post} /v1/base/tool-category/delete 删除素材分类（软删除）
     * @apiGroup ToolCategory
     * @apiName delete
     * @apiUse CommonParam
     *
     * @apiBody {String} id         ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $service = new ToolCategoryService();
        return $service->deleteToolCategory($params);
    }
}
